package com.tsu.mall.admin.controller;

import com.tsu.mall.admin.entity.Attendant;
import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.VO.ResultVO;
import com.tsu.mall.common.entity.Member;
import com.tsu.mall.common.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//会员管理
@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private IMemberService iMemberService;
    @GetMapping("/memberPage")
    public String memberPage(){
        return "member";
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO list(Integer page, Integer limit, String memberNumber,String memberPhone) {
        return iMemberService.doList(page,limit,memberNumber,memberPhone);
    }

    //上传头像功能
    @GetMapping("/avatar")
    @ResponseBody
    public LayuiTableVO avatar(MultipartFile file){
        String fileName=System.currentTimeMillis()+".jpg";
        File f=new File("D:/test/"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        LayuiTableVO vo=new LayuiTableVO();

        String s="/upload/"+fileName;
        Map<String ,String> map=new HashMap<>();
        map.put("avatar",s);
        vo.setCode(0);
        vo.setData(map);
        return vo;
    }
    @PostMapping("/updatePassword")
    @ResponseBody
    public void updatePassword(Integer id){
        iMemberService.updatePassword(id);
    }

    @PostMapping("/selectById")
    @ResponseBody
    public LayuiTableVO selectById(Integer id){
        return iMemberService.selectById(id);
    }

    @PostMapping("/updateStatus")
    @ResponseBody
    public ResultVO save(Integer status,Integer id){
        return iMemberService.updateByStatus(status,id);
    }

}
